Counting Things¶
Key Ideas¶
- Counting a predefined set of values
- Counting a dynamic set of values
👩🏽🎨 How many vowels?¶
Write a program that takes a string and prints out the number of each vowel found in the string.
% python vowel_counts.py 'Hello. How are you?'
{'a': 1, 'e': 2, 'i': 0, 'o': 3, 'u': 1}
vowel_counts.py
¶
👨🏾🎨 States¶
Write a program that polls the user for their homestate.
Print out a count of each state mentioned.
% python state_count.py
State: Utah
State: Idaho
State: New Jersey
State: Oregon
State: Utah
State:
{'Utah': 2, 'Idaho': 1, 'New Jersey': 1, 'Oregon': 1}
state_count.py
¶
🖌 Punctuation¶
In [1]:
import string
In [2]:
string.punctuation
Out[2]:
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
In [8]:
tokens = []
for word in 'This is a sentence. This is another sentence!?'.split():
word = word.strip(string.punctuation)
tokens.append(word)
print(' '.join(tokens))
This is a sentence This is another sentence
🧑🏻🎨 Many, many words¶
Write a program that takes a filename and counts the number of each individual word in that file.
Ignore case and punctuation.
Key Ideas¶
- Counting a predefined set of values
- Counting a dynamic set of values